package com.example.goodsui.service;

import com.example.goodscommons.pojo.Book;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@RestController
public class GoodsUIServiceImpl implements GoodsUIService{
    @Resource
    private RestTemplate restTemplate;

    @Override
    public void testGoodsProvider() {
        System.out.println("1111111");
        String object = restTemplate.getForObject("http://goods-provider/provider/test", String.class);
        System.out.println("goods-provider返回的结果"+object);

    }

    @Override
    public List<Book> getBookList() {
        Book[] bookList = restTemplate.getForObject("http://goods-provider/provider/list", Book[].class);
        System.out.println(bookList[0].getId());
        System.out.println(bookList[1].getId());
        return Arrays.asList(bookList);
    }

    @Override
    @HystrixCommand(
            fallbackMethod = "fallbackBookDetail",commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeInMilliseconds",value = "1000")
    }
    )
    public Book getBookDetail(Integer id) {
        Book book = restTemplate.getForObject("http://goods-provider/provider/detail/"+id, Book.class);
        return book;
    }
    public Book fallbackBookDetail(Integer id) {
        return new Book();
    }

    @Override
    public void addBook(Book book) {
        restTemplate.postForObject("http://goods-provider/provider/add",book,String.class);
    }
}
